package com.sh.data.engine.web.webapi.controller.datadev;

import com.sh.data.engine.application.model.dto.datadev.OfflineDevQualityFieldResponse;
import com.sh.data.engine.application.model.dto.datadev.OfflineDevQualityTableResponse;
import com.sh.data.engine.application.model.request.datadev.OfflineDevDataQualityRuleRequest;
import com.sh.data.engine.application.service.datadev.OfflineDevDataQualityApplicationService;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.datadev.offline.model.domain.OfflineDevDataQualityDetailDomain;
import com.sh.data.engine.domain.dataquality.model.domain.DataQualityRuleTemplateDomain;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: mengzheng.mhc
 * @date: 2025/9/11 9:13
 */
@Validated
@RestController
@RequestMapping("/offlineDev/dataquality")
@Tag(name = "数据开发-离线开发-同步任务", description = "离线开发相关-数据质量API")
@Slf4j
public class OfflineDevDataQualityController {

    @Autowired
    private OfflineDevDataQualityApplicationService offlineDevDataQualityApplicationService;

    @Operation(summary = "获取数据表列表")
    @GetMapping("/tables")
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 0)
    public RestResponseEntity<List<OfflineDevQualityTableResponse>> getTables() {
        var result = offlineDevDataQualityApplicationService.getTables();
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "获取数据表的字段列表")
    @GetMapping("/fields")
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 0)
    public RestResponseEntity<List<OfflineDevQualityFieldResponse>> getFields(
        @Parameter(description = "表名称", required = true) @RequestParam String tableName) {
        var result = offlineDevDataQualityApplicationService.getFields(tableName);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getRuleTemplates")
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 0)
    @Operation(summary = "获取模版规则")
    public RestResponseEntity<List<DataQualityRuleTemplateDomain>> getRuleTemplates(
        @Parameter(description = "规则类型,0-单表 1-多表", schema = @Schema(allowableValues = {"0", "1"}), required = true)
        @RequestParam Integer templateTableType) {
        var result = offlineDevDataQualityApplicationService.getRuleTemplates(templateTableType);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/saveRule")
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1)
    @Operation(summary = "保存规则")
    @LogExecution(
        business = Business.离线开发数据质量,
        operation = EngineOperation.ADD_UPDATE)
    public RestResponseEntity<Boolean> saveRule(@Valid @RequestBody OfflineDevDataQualityRuleRequest dataQualityRule) {
        var result = offlineDevDataQualityApplicationService.saveRule(dataQualityRule);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getRule")
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 0)
    @Operation(summary = "获取规则详情")
    public RestResponseEntity<OfflineDevDataQualityDetailDomain> getRule(
        @Parameter(description = "nodeId", required = true) @RequestParam Long nodeId) {
        var result = offlineDevDataQualityApplicationService.getRule(nodeId);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/runRule")
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1)
    @Operation(summary = "运行规则")
    @LogExecution(
        business = Business.离线开发数据质量,
        operation = EngineOperation.START_TASK)
    public RestResponseEntity<Long> run(
        @Parameter(description = "nodeId", required = true) @RequestParam Long nodeId) {
        return RestResponseEntity.success(offlineDevDataQualityApplicationService.run(nodeId));
    }

    @GetMapping("/stop")
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1)
    @Operation(summary = "停止运行规则")
    @LogExecution(
        business = Business.离线开发数据质量,
        operation = EngineOperation.STOP_TASK)
    public RestResponseEntity<Boolean> stop(
        @Parameter(description = "jobId", required = true) @RequestParam Long jobId) {
        var result = offlineDevDataQualityApplicationService.stop(jobId);
        return RestResponseEntity.success(result);
    }
}
